package com.gmrz.uap.jedis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gmrz.uap.cache.AmmsCacheUtil;
import com.gmrz.uap.cfg.SystemConst;
import com.gmrz.uap.common.ControllerSupport;
import com.gmrz.uap.jfinal.interceptor.SuperAdminAuthInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class JedisController extends ControllerSupport {

    @Before(SuperAdminAuthInterceptor.class)
    public void index() {

    }

    public void list() {
        Jedis jedis = AmmsCacheUtil.getCacheStoreService().getJedis();
        Set<String> keySet = jedis.keys("*");
        List<Record> recordList = new ArrayList<Record>();
        for (String value : keySet) {
            Record record = new Record();
            record.set("key", value);
            recordList.add(record);
        }
        jedis.close();
        render(responseJsonRender(recordList));
    }

    public void flushDB() {
        Jedis jedis = AmmsCacheUtil.getCacheStoreService().getJedis();
        String res = jedis.flushDB();
        AmmsCacheUtil.getCacheStoreService().returnJedis(jedis);
        render(responseJsonRender(res));
    }

    public void find() {
        String key = getPara("key");
        Jedis jedis = null;
        try {
            jedis = AmmsCacheUtil.getCacheStoreService().getJedis();
            System.out.println(jedis.type(key));
            String res = "";
            if (StrKit.equals(jedis.type(key), "string")) {
                res = jedis.get(key);
            } else if (StrKit.equals(jedis.type(key), "set")) {
                Set<String> result = jedis.smembers(key);
                res = JSON.toJSONString(result);
            } else if (StrKit.equals(jedis.type(key), "hash")) {
                Map<String, String> result = jedis.hgetAll(key);
                res = JSON.toJSONString(result);
            }

            AmmsCacheUtil.getCacheStoreService().returnJedis(jedis);
            render(responseJsonRender(res));
        } catch (Exception e) {
            AmmsCacheUtil.getCacheStoreService().returnJedis(jedis);
            e.printStackTrace();
        }
    }

    public void add() {
        render("form" + SystemConst.VIEW_EXTENDSION);
    }

    public void save() {
        String key = getPara("key");
        String value = getPara("value");
        Jedis jedis = AmmsCacheUtil.getCacheStoreService().getJedis();
        String res = jedis.set(key, value);
        AmmsCacheUtil.getCacheStoreService().returnJedis(jedis);
        render(responseJsonRender(res));
    }

    public void edit() {
        Jedis jedis = AmmsCacheUtil.getCacheStoreService().getJedis();
        String key = getPara("key");
        String value = jedis.get(key);

        setAttr("key", key);
        setAttr("value", value);
        render("form" + SystemConst.VIEW_EXTENDSION);

    }

    public void update() {
        String key = getPara("key");
        String value = getPara("value");
        Jedis jedis = AmmsCacheUtil.getCacheStoreService().getJedis();
        String res = jedis.set(key, value);
        AmmsCacheUtil.getCacheStoreService().returnJedis(jedis);
        render(responseJsonRender(res));
    }

    public void remove() {
        String key = getPara("key");
        Jedis jedis = AmmsCacheUtil.getCacheStoreService().getJedis();
        long count = jedis.del(key);
        AmmsCacheUtil.getCacheStoreService().returnJedis(jedis);
        render(responseJsonRender(count > 0));
    }
}
